package com.zhuozhou.jdyc.main.meinfo.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.RegexUtils;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityBindingPhoneBinding;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;

/**
 * Create by Once_ChuXy on 2021/10/11
 * Describe:
 */
public class BindingPhoneActivity extends BaseActivity<MeInfoViewModel, ActivityBindingPhoneBinding> implements TextWatcher {

    TimeCount timer;

    @Override
    public String setTitleBar() {
        return "绑定手机";
    }//注册

    @Override
    protected int getLayoutId() {
        return R.layout.activity_binding_phone;
    }

    @Override
    public void initView() {
        binding.userEdit.addTextChangedListener(this);
        binding.phoneNumberButton.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.userEdit.getText().toString())) {
                ToastUtils.showShort(this, "请填写手机号码");
                return;
            }
            if (!RegexUtils.isMobile(binding.userEdit.getText().toString())) {
                LogUtil.show(binding.userEdit.toString());
                ToastUtils.showShort(this, "手机号码不正确");
                return;
            }
            mViewModel.getDanXin(binding.userEdit.getText().toString(), UserAccountHelper.getUserID() + "");
        });
        binding.loginSubmit.setOnClickListener(v -> {
            if (StringUtil.isEmpty(binding.userEdit.getText().toString())) {
                ToastUtils.showShort(this, "请填写手机号码");
                return;
            }
            if (!RegexUtils.isMobile(binding.userEdit.getText().toString())) {
                ToastUtils.showShort(this, "手机号码不正确");
                return;
            }
            if (StringUtil.isEmpty(binding.codeEdit.getText().toString())) {
                ToastUtils.showShort(this, "请填写验证码");
                return;
            }
            if (StringUtil.isEmpty(binding.passwordEdit.getText().toString())) {
                ToastUtils.showShort(this, "请输入密码");
                return;
            }
            mViewModel.getVerify(binding.codeEdit.getText().toString(), binding.userEdit.getText().toString(), UserAccountHelper.getUserID() + "");
        });

        mViewModel.DanXinMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(this, "已发送");
            timer = new TimeCount(60000, 1000);
            timer.start();
        });
        mViewModel.VerifyMutableLiveData.observe(this, v -> {
            if (v == 0) {
                ToastUtils.showShort(this, "验证码超时");
            } else {
                mViewModel.getBangDing(binding.passwordEdit.getText().toString(), binding.userEdit.getText().toString(), UserAccountHelper.getUserID() + "");
            }
        });
        mViewModel.BangDingMutableLiveData.observe(this, v -> {
            ToastUtils.showShort(this, v.getMsg());
            if (v.getType() == 1) {
                finish();
            }
        });
    }

    @Override
    public void initData(Bundle bundle) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (RegexUtils.isMobile(s.toString())) {
            binding.phoneNumberButton.setClickable(true);
            binding.phoneNumberButton.setTextColor(getResources().getColor(R.color.themeColor));
        } else {
            binding.phoneNumberButton.setClickable(false);
            binding.phoneNumberButton.setTextColor(getResources().getColor(R.color.register_edit_color));
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    /**
     * 获取验证码倒计时
     */
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            binding.phoneNumberButton.setText("重新获取");
            binding.phoneNumberButton.setClickable(true);
            binding.phoneNumberButton.setTextColor(getResources().getColor(R.color.themeColor));
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            binding.phoneNumberButton.setText("重新发送(" + millisUntilFinished / 1000 + ")秒");
            binding.phoneNumberButton.setClickable(false);
            binding.phoneNumberButton.setTextColor(getResources().getColor(R.color.register_edit_color));
        }
    }

}